package android.slc.appbase.ui.vd.wrapper;

import android.slc.appbase.api.slc.callback.SlcObserver;
import android.slc.adapter.LoadMoreDataFormatByFx;
import android.slc.adapter.PageModel;

import java.io.Serializable;

/**
 * @Description:
 * @Author: sunlunchang
 * @CreateDate: 2022/10/13 上午 10:25
 */
public abstract class BaseListPageByFxWrapper<T extends Serializable> extends BaseListCommonWrapper<PageModel<T>> implements IBaseListPageByFxWrapper<T> {
    public final LoadMoreDataFormatByFx<T> loadMoreDataFormatByFx;

    public BaseListPageByFxWrapper(IBaseListPageByFxWrapper<T> onResultsListener) {
        super(onResultsListener);
        this.loadMoreDataFormatByFx = createLoadMoreDataFormatByFx();
    }

    protected LoadMoreDataFormatByFx<T> createLoadMoreDataFormatByFx() {
        return new LoadMoreDataFormatByFx<>();
    }

    @Override
    public LoadMoreDataFormatByFx<T> getLoadMoreDataFormatByFx() {
        return loadMoreDataFormatByFx;
    }


    public void refresh() {
        loadMoreDataFormatByFx.refresh();
    }


    @Override
    public final void loadMore() {
        //getLoadMoreDataFormatByFx().startLoadMoreEnd();
        onLoadMore();
    }
    protected abstract void  onLoadMore();

    @Override
    public SlcObserver<PageModel<T>> getDataListSlcObserver() {
        throw new RuntimeException("不要用这个，没有意义");
    }

    @Override
    public void onSucceed(PageModel<T> data) {
        super.onSucceed(data);
        this.loadMoreDataFormatByFx.loadMoreComplete(data);
    }

    @Override
    public void onError(int currentPageIndex, int errorCode, String errorMessage) {
        this.loadMoreDataFormatByFx.loadMoreFail(currentPageIndex, errorCode, errorMessage);
        if (this.onResultsListener instanceof OnResultsByFxListener) {
            OnResultsByFxListener<PageModel<T>> onResultsByFxListener = (OnResultsByFxListener<PageModel<T>>) this.onResultsListener;
            onResultsByFxListener.onError(currentPageIndex, errorCode, errorMessage);
        }
    }
}
